package com.springboot.blog.interceptor;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.springboot.blog.entity.Menu;
import com.springboot.blog.entity.Role;
import com.springboot.blog.entity.User;
import com.springboot.blog.entity.UserRole;
import com.springboot.blog.service.IMenuService;
import com.springboot.blog.service.IRoleService;
import com.springboot.blog.service.IUserRoleService;
import com.springboot.blog.service.IUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

public class CustomRealm extends AuthorizingRealm {
    @Autowired
    IUserService userService;
    @Autowired
    IUserRoleService userRoleService;
    @Autowired
    IRoleService roleService;
    @Autowired
    IMenuService menuService;
    /**
     * @MethodName doGetAuthorizationInfo
     * @Description 权限配置类
     * @Param [principalCollection]
     * @Return AuthorizationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        User user = (User) principalCollection.getPrimaryPrincipal();
        List<UserRole> userRoles = userRoleService.list(new LambdaQueryWrapper<UserRole>().eq(UserRole::getUserId, user.getId()));
        List<Integer> roleIds=new ArrayList<>();
        for (UserRole r:userRoles){
            roleIds.add(r.getRoleId());
        }
        List<Role> roles = roleService.list(new LambdaQueryWrapper<Role>().in(Role::getId, roleIds));
        List<String> menuIds=new ArrayList<>();
        for(Role r:roles){
            String rules = r.getRules();
            String[] split = rules.split(",");
            menuIds.addAll(Arrays.asList(split));
        }

        HashSet<String> h = new HashSet<>(menuIds);
        menuIds.clear();
        menuIds.addAll(h);

        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        List<Menu> menus = menuService.list(new LambdaQueryWrapper<Menu>().in(Menu::getId, menuIds));
        for (Menu m:menus){
            simpleAuthorizationInfo.addStringPermission(m.getPrem());
        }
        return simpleAuthorizationInfo;
    }

    /**
     * @MethodName doGetAuthenticationInfo
     * @Description 认证配置类
     * @Param [authenticationToken]
     * @Return AuthenticationInfo
     * @Author WangShiLin
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //获取用户信息
        String name = authenticationToken.getPrincipal().toString();
        User user = userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUsername, name));
        if(user==null){return null;}
        return new SimpleAuthenticationInfo(user, user.getPassword(), getName());
    }
}
